import { Result } from "../result";

/**
 * 样式值化
 */
export abstract class CssValue<T> {
  protected valueResult: Result<string>;

  constructor(v: T | undefined) {
    if (v) {
      this.valueResult = this.parseCssValue(v);
    } else {
      this.valueResult = Result.fail("初始化属性值为空");
    }
  }

  // 如果解析失败,则返回undefined
  cssValue(): string | undefined {
    return this.valueResult.getResult();
  }
  getLevel(_level: number) {
    const res = this.valueResult.getResult();
    if (res) {
      console.log(res);
    }
  }
  // 如果失败才返回错误信息,否则返回undefined
  cssValueError(): string | undefined {
    if (this.valueResult && !this.valueResult.isSuccess()) {
      return this.valueResult.getCode().getMsgDeault();
    }
    return undefined;
  }

  protected abstract parseCssValue(v: T): Result<string>;
}

export function numberMaybePercentage(num: number) {
  let result: string;
  if (num < 1) {
    result = num * 100 + "%";
  } else {
    result = num + "";
  }
  return result;
}
